package com.telit.firecommand.controller;

import com.telit.common.entity.DataReturn;
import com.telit.common.enums.ResultCode;
import com.telit.firecommand.entity.*;
import com.telit.firecommand.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author zhouyu
 * @description 历史火情接口
 */
@RestController
@RequestMapping("/history-fire")
@Api(tags = "历史火情接口", description = "/history-fire")
public class HistoryFireController {

    @Autowired
    private HistoryFireService historyFireService;

    @Autowired
    private FireHandleService fireHandleInfoService;

    @Autowired
    private FireQueryService fireQueryService;

    @Autowired
    private FireWeatherInfoService fireWeatherInfoService;

    @Autowired
    private ICommFireOnemapService commFireOnemapService;

    @PostMapping("/addFireLoss")
    @ApiOperation("添加历史火情信息")
    public DataReturn addFireLoss(@RequestBody CommHistoryFire commHistoryFire) {
        if (commHistoryFire == null) {
            return DataReturn.failure(ResultCode.FORM_PARAMS_CHECK_ERROR);
        }
        historyFireService.addFireLoss(commHistoryFire);
        return DataReturn.ok();
    }

/*    @GetMapping("/addFireTrack")
    @ApiOperation("添加火烧基地信息")
    public DataReturn addFireTrack(@RequestParam Long id,@RequestParam String fireArea,@RequestParam String firePoint){
        fireLossService.addFireTrack(id,fireArea,firePoint);
        return DataReturn.ok();
    }*/

    @GetMapping("/deleteById")
    @ApiOperation("删除历史火情信息")
    public DataReturn deleteById(@RequestParam Long id) {
        if (id == null) {
            return DataReturn.failure(ResultCode.FORM_PARAMS_CHECK_ERROR);
        }
        //删除火情处理信息
        CommHistoryFire commHistoryFire = historyFireService.queryById(id);
        fireHandleInfoService.deleteByFireId(commHistoryFire.getFireId());

        historyFireService.deleteById(id);

        return DataReturn.ok();
    }

/*    @PostMapping("/updateById")
    @ApiOperation("修改灾损统计信息")
    public DataReturn updateById(@RequestBody CommFireLoss commFireLoss){
        if (commFireLoss==null){
            return DataReturn.failure(ResultCode.FORM_PARAMS_CHECK_ERROR);
        }
        fireLossService.updateById(commFireLoss);

        //更新火情处理信息
        CommFireHandle fireHandleInfo=commFireLoss.getFireHandleInfo();
        fireHandleInfoService.updateData(fireHandleInfo);
        return DataReturn.ok();
    }*/

    @GetMapping("/queryById")
    @ApiOperation("根据id查询历史火情信息")
    public DataReturn queryById(@RequestParam Long id) {
        if (id == null) {
            return DataReturn.failure(ResultCode.FORM_PARAMS_CHECK_ERROR);
        }
        CommHistoryFire commHistoryFire = historyFireService.queryById(id);
        //以下注释代码在上一步通过ID查询火情级别的时候已经赋值
        /*if(ObjectUtil.isEmpty(commHistoryFire)){
            commHistoryFire=new CommHistoryFire();
        }*/
        //火情处置信息设置
        setFirHandleInfo(id, commHistoryFire);
        //火情详细信息设置
        CommFireAlarm commFireAlarm = setFireAlarmInfo(id, commHistoryFire);
        //火情一张图信息设置
        setFireOnemap(commFireAlarm.getLongitude(), commFireAlarm.getLatitude(), commHistoryFire);
        //天气信息设置
        setWeatherInfo(id, commHistoryFire);
        //额外信息设置
        setExtInfo(commHistoryFire);
        //返回数据
        return DataReturn.ok(commHistoryFire);

        /*以上代码为此以下代码的结构优化，以下代码太过雍余
        if (id==null){
            return DataReturn.failure(ResultCode.FORM_PARAMS_CHECK_ERROR);
        }
        CommHistoryFire commHistoryFire = historyFireService.queryById(id);
        if (commHistoryFire !=null) {
            CommFireHandle fireHandleInfo = fireHandleInfoService.selectInfoByfireId(commHistoryFire.getFireId());
            if (fireHandleInfo!=null){
                commHistoryFire.setCommanderInChief(fireHandleInfo.getCommanderInChief());
                commHistoryFire.setCommanderInChiefDuty(fireHandleInfo.getCommanderInChiefDuty());
                commHistoryFire.setFrontlineCommand(fireHandleInfo.getFrontlineCommand());
                commHistoryFire.setFrontlineCommandDuty(fireHandleInfo.getFrontlineCommandDuty());
                commHistoryFire.setRearCommand(fireHandleInfo.getRearCommand());
                commHistoryFire.setRearCommandDuty(fireHandleInfo.getRearCommandDuty());
            }
            CommFireAlarm commFireAlarm = fireQueryService.queryById(id);
            CommFireOnemap commFireOnemap = commFireOnemapService.findByPoint(commFireAlarm.getLongitude(), commFireAlarm.getLatitude());
            commHistoryFire.setFireId(id);
            commHistoryFire.setLongitude(commFireAlarm.getLongitude());
            commHistoryFire.setLatitude(commFireAlarm.getLatitude());
            commHistoryFire.setEndTime(commFireAlarm.getEndTime());
            commHistoryFire.setDetailAddress(commFireAlarm.getDetailAddress());
            commHistoryFire.setDistrictName(commFireAlarm.getDistrictName());
            commHistoryFire.setFindTime(commFireAlarm.getFindTime());
            commHistoryFire.setAltitude(commFireAlarm.getAltitude());
            if(commFireOnemap !=null) {
                commHistoryFire.setSlopedirection(commFireOnemap.getPoXiang());
                commHistoryFire.setSlopeposition(commFireOnemap.getPoWei());
                commHistoryFire.setSlope(commFireOnemap.getPoDu());
                commHistoryFire.setLandtype(commFireOnemap.getDiLei());
                commHistoryFire.setForestCategory(commFireOnemap.getSenLinLb());
                commHistoryFire.setAgeGroup(commFireOnemap.getLingZu());
                commHistoryFire.setDominantTreeSpecies(commFireOnemap.getYouShiSz());
            }
            commHistoryFire.setTreeSpeciesComposition(null);
            commHistoryFire.setFireLevel(commFireAlarm.getFireLevel());
            FireWeatherInfo fireWeatherInfo = fireWeatherInfoService.queryByFireId(id);
            commHistoryFire.setWeatherCondition(fireWeatherInfo.getTextDay());
            commHistoryFire.setMaximumTemperature(fireWeatherInfo.getHigh());
            commHistoryFire.setMinimumTemperature(fireWeatherInfo.getLow());
            commHistoryFire.setMinimumHumidity(fireWeatherInfo.getHumidity());
            commHistoryFire.setWindDirection(fireWeatherInfo.getWindDirection());
            commHistoryFire.setWindLevel(fireWeatherInfo.getWindScale());
            commHistoryFire.setWindSpeed(fireWeatherInfo.getWindSpeed());
            commHistoryFire.setRainfall(fireWeatherInfo.getRainfall());
            return DataReturn.ok(commHistoryFire);
        }else {
            commHistoryFire =new CommHistoryFire();
            CommFireAlarm commFireAlarm = fireQueryService.queryById(id);
            CommFireOnemap commFireOnemap = commFireOnemapService.findByPoint(commFireAlarm.getLongitude(), commFireAlarm.getLatitude());
            commHistoryFire.setFireId(id);
            commHistoryFire.setLongitude(commFireAlarm.getLongitude());
            commHistoryFire.setLatitude(commFireAlarm.getLatitude());
            commHistoryFire.setDetailAddress(commFireAlarm.getDetailAddress());
            commHistoryFire.setDistrictName(commFireAlarm.getDistrictName());
            commHistoryFire.setFindTime(commFireAlarm.getFindTime());
            commHistoryFire.setAltitude(commFireAlarm.getAltitude());
            CommFireHandle fireHandleInfo = fireHandleInfoService.selectInfoByfireId(commHistoryFire.getFireId());
            if (fireHandleInfo!=null){
                commHistoryFire.setCommanderInChief(fireHandleInfo.getCommanderInChief());
                commHistoryFire.setCommanderInChiefDuty(fireHandleInfo.getCommanderInChiefDuty());
                commHistoryFire.setFrontlineCommand(fireHandleInfo.getFrontlineCommand());
                commHistoryFire.setFrontlineCommandDuty(fireHandleInfo.getFrontlineCommandDuty());
                commHistoryFire.setRearCommand(fireHandleInfo.getRearCommand());
                commHistoryFire.setRearCommandDuty(fireHandleInfo.getRearCommandDuty());
            }
            if(commFireOnemap !=null) {
                commHistoryFire.setSlopedirection(commFireOnemap.getPoXiang());
                commHistoryFire.setSlopeposition(commFireOnemap.getPoWei());
                commHistoryFire.setSlope(commFireOnemap.getPoDu());
                commHistoryFire.setLandtype(commFireOnemap.getDiLei());
                commHistoryFire.setForestCategory(commFireOnemap.getSenLinLb());
                commHistoryFire.setAgeGroup(commFireOnemap.getLingZu());
                commHistoryFire.setDominantTreeSpecies(commFireOnemap.getYouShiSz());
            }
            commHistoryFire.setTreeSpeciesComposition(null);
            commHistoryFire.setFireLevel(commFireAlarm.getFireLevel());
            FireWeatherInfo fireWeatherInfo = fireWeatherInfoService.queryByFireId(id);
            if(fireWeatherInfo != null){
                commHistoryFire.setWeatherCondition(fireWeatherInfo.getTextDay());
                commHistoryFire.setMaximumTemperature(fireWeatherInfo.getHigh());
                commHistoryFire.setMinimumTemperature(fireWeatherInfo.getLow());
                commHistoryFire.setMinimumHumidity(fireWeatherInfo.getHumidity());
                commHistoryFire.setWindDirection(fireWeatherInfo.getWindDirection());
                commHistoryFire.setWindLevel(fireWeatherInfo.getWindScale());
                commHistoryFire.setWindSpeed(fireWeatherInfo.getWindSpeed());
                commHistoryFire.setRainfall(fireWeatherInfo.getRainfall());
            }
            return DataReturn.ok(commHistoryFire);
        }*/
    }

    private void setExtInfo(CommHistoryFire commHistoryFire) {
        if (commHistoryFire != null) {
            commHistoryFire.setTreeSpeciesComposition(null);
        }
    }

    private void setWeatherInfo(Long id, CommHistoryFire commHistoryFire) {
        FireWeatherInfo fireWeatherInfo = fireWeatherInfoService.queryByFireId(id);
        if (commHistoryFire != null && fireWeatherInfo != null) {
            commHistoryFire.setWeatherCondition(fireWeatherInfo.getTextDay());
            commHistoryFire.setMaximumTemperature(fireWeatherInfo.getHigh());
            commHistoryFire.setMinimumTemperature(fireWeatherInfo.getLow());
            commHistoryFire.setMinimumHumidity(fireWeatherInfo.getHumidity());
            commHistoryFire.setWindDirection(fireWeatherInfo.getWindDirection());
            commHistoryFire.setWindLevel(fireWeatherInfo.getWindScale());
            commHistoryFire.setWindSpeed(fireWeatherInfo.getWindSpeed());
            commHistoryFire.setRainfall(fireWeatherInfo.getRainfall());
        }
    }

    private void setFireOnemap(String longitude, String latitude, CommHistoryFire commHistoryFire) {
        CommFireOnemap commFireOnemap = commFireOnemapService.findByPoint(longitude, latitude);
        if (commHistoryFire != null && commFireOnemap != null) {
            commHistoryFire.setSlopedirection(commFireOnemap.getPoXiang());
            commHistoryFire.setSlopeposition(commFireOnemap.getPoWei());
            commHistoryFire.setSlope(commFireOnemap.getPoDu());
            commHistoryFire.setLandtype(commFireOnemap.getDiLei());
            commHistoryFire.setForestCategory(commFireOnemap.getSenLinLb());
            commHistoryFire.setAgeGroup(commFireOnemap.getLingZu());
            commHistoryFire.setDominantTreeSpecies(commFireOnemap.getYouShiSz());
        }
    }

    private CommFireAlarm setFireAlarmInfo(Long id, CommHistoryFire commHistoryFire) {
        CommFireAlarm commFireAlarm = fireQueryService.queryById(id);
        if (commHistoryFire != null && commFireAlarm != null) {
            commHistoryFire.setFireId(id);
            commHistoryFire.setLongitude(commFireAlarm.getLongitude());
            commHistoryFire.setLatitude(commFireAlarm.getLatitude());
            commHistoryFire.setEndTime(commFireAlarm.getEndTime());
            commHistoryFire.setDetailAddress(commFireAlarm.getDetailAddress());
            commHistoryFire.setDistrictName(commFireAlarm.getDistrictName());
            commHistoryFire.setFindTime(commFireAlarm.getFindTime());
            commHistoryFire.setAltitude(commFireAlarm.getAltitude());
            //前面的业务中已经设置了火情级别，这里重新设置的意义何在，暂时注释掉
//            commHistoryFire.setFireLevel(commFireAlarm.getFireLevel());
        }
        return commFireAlarm;
    }

    private void setFirHandleInfo(Long id, CommHistoryFire commHistoryFire) {
        CommFireHandle fireHandleInfo = fireHandleInfoService.selectInfoByfireId(id);
        if (commHistoryFire != null && fireHandleInfo != null) {
            commHistoryFire.setCommanderInChief(fireHandleInfo.getCommanderInChief());
            commHistoryFire.setCommanderInChiefDuty(fireHandleInfo.getCommanderInChiefDuty());
            commHistoryFire.setFrontlineCommand(fireHandleInfo.getFrontlineCommand());
            commHistoryFire.setFrontlineCommandDuty(fireHandleInfo.getFrontlineCommandDuty());
            commHistoryFire.setRearCommand(fireHandleInfo.getRearCommand());
            commHistoryFire.setRearCommandDuty(fireHandleInfo.getRearCommandDuty());
        }
    }

}
